package com.example.gzysmartcity.viewmodel;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ThreadUtils;
import com.example.gzysmartcity.repository.model.Personnel;
import com.example.gzysmartcity.repository.remote.State;
import com.example.gzysmartcity.utils.NetworkUtil;

import java.util.List;
import java.util.stream.Collectors;

public class PersonnelViewModel extends ViewModel {
    MutableLiveData<State> personnelLiveData = new MutableLiveData<>();
    List<Personnel.DataBean> personnelList;

    public void getPersonnelData(int id) {
        ThreadUtils.executeByCached(new ThreadUtils.Task<Personnel>() {
            @Override
            public Personnel doInBackground() throws Throwable {
                return NetworkUtil.getInstance().get("Personnel/getAll?", null, Personnel.class, 0);
            }

            @Override
            public void onSuccess(Personnel result) {
                personnelList = result.getData().stream().filter(a -> a.getType() == id).collect(Collectors.toList());
                personnelLiveData.postValue(State.SUCCESS);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onFail(Throwable t) {
                personnelLiveData.postValue(State.FAIL);
            }
        });
    }

    public List<Personnel.DataBean> getPersonnelsSreach(String p) {
        if (p.equals(""))
            return personnelList;
        int p1 = Integer.parseInt(p) + 500;
        int p2 = Integer.parseInt(p) - 500;
        return personnelList.stream().filter(a -> a.getPrice() >= p2 && a.getPrice() <= p1).collect(Collectors.toList());
    }

    public MutableLiveData<State> getPersonnelLiveData() {
        return personnelLiveData;
    }

    public List<Personnel.DataBean> getPersonnelList() {
        return personnelList;
    }
}
